package com.yhp.web;

import com.yhp.service.UsersService;
import com.yhp.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/checkuname")
public class CheckUserNameServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收数据
        String uname = req.getParameter("uname");
        //判断名字是否存在
        //注意：在使用ajax时，不需要考虑跳页面，默认返回到ajax发送的页面
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        UsersService usersService = new UsersServiceImpl();
        boolean checkuser = usersService.checkuser(uname);
        if(checkuser){
            //用户名已存在
            writer.print("用户名已存在");
        }else{
            //用户名可用
            writer.print("用户名可用");
        }
    }


}
